package tw.com.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import tw.com.to.CDI_TODO;

@Repository
public class CDI_TODO_DAOImpl implements CDI_TODO_DAO {

	@Autowired
	private SessionFactory sessionFactory;

	@Transactional
	public long getCountByCDIID(String CDIID) {
		long count = 0;

		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				CDI_TODO.class);

		criteria.add(Restrictions.eq("CDIID", CDIID));

		criteria.setProjection(Projections.rowCount());

		count = (Long) criteria.list().get(0);

		return count;
	}

	@Transactional
	public List<CDI_TODO> getByCDIID(String cdiid) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				CDI_TODO.class);

		criteria.add(Restrictions.eq("CDIID", cdiid));
		List<CDI_TODO> todoList = criteria.list();
		
		return todoList;
	}

	@Transactional
	public List<CDI_TODO> getByStatetype5AndCDIID(String cdiid,
			String statetype5) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				CDI_TODO.class);

		criteria.add(Restrictions.eq("CDIID", cdiid));
		criteria.add(Restrictions.like("StateType5", "%"+statetype5+"%"));

		List<CDI_TODO> todoList = criteria.list();
		return todoList;
	}

	@Transactional
	public int getCountByStatetype5AndCDIID(String cdiid, String statetype5) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				CDI_TODO.class);

		criteria.add(Restrictions.eq("CDIID", cdiid));
		criteria.add(Restrictions.eq("StateType5", statetype5));

		List<CDI_TODO> todoList = criteria.list();
		return todoList.size();
	}

}
